package cn.donting.jme.expansion.inspect;

import cn.donting.jme.EditorContent;
import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.TitlePaneAttributeGroup;
import cn.donting.jme.expansion.attribute.lmpl.SelectAttribute;
import cn.donting.jme.expansion.builder.mesh.MeshAttributeBuilder;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.ioc.Sort;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;

import java.util.ArrayList;
import java.util.List;

@Bean
@Sort(2)
public class MeshInspectBuilder implements InspectBuilder<Geometry> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof Geometry;
    }

    @Override
    public void builder(Inspect inspect, Geometry geometry) {
        Mesh mesh = geometry.getMesh();

        TitlePaneAttributeGroup titlePaneAttributeGroup = new TitlePaneAttributeGroup("网格");
        titlePaneAttributeGroup.setTitleIcon(new FlatSVGIcon("icon/mesh.svg",16,16));
        List<Attribute> attributes = new ArrayList<>();
        List<MeshAttributeBuilder> meshAttributeBuilders = EditorContent.getIocContainer().getBeansByTypes(MeshAttributeBuilder.class);

        SelectAttribute<Mesh> meshSelectAttribute = new SelectAttribute("网格类型: ");
        for (MeshAttributeBuilder meshAttributeBuilder : meshAttributeBuilders) {
            Mesh newMesh = meshAttributeBuilder.newInstance();
            if (newMesh.getClass().equals(mesh.getClass())) {
                meshSelectAttribute.addOption(meshAttributeBuilder.getName(), mesh,meshAttributeBuilder.getIcon(SelectAttribute.Option.ICON_SIZE));
            } else {
                meshSelectAttribute.addOption(meshAttributeBuilder.getName(), newMesh,meshAttributeBuilder.getIcon(SelectAttribute.Option.ICON_SIZE));
            }
        }
        meshSelectAttribute.setValue(mesh);
        attributes.add(meshSelectAttribute);
        //切换 mesh
        meshSelectAttribute.setValueChangeListener(false, newValue -> {
            titlePaneAttributeGroup.clear();
            attributes.clear();
            attributes.add(meshSelectAttribute);
            createMeshAttribute(newValue, attributes, meshAttributeBuilders);
            titlePaneAttributeGroup.addAttribute(attributes);
            EditorContent.runJme(() -> {
                geometry.setMesh(newValue);
            });

        });
        createMeshAttribute(mesh, attributes, meshAttributeBuilders);
        titlePaneAttributeGroup.addAttribute(attributes);

        inspect.addAttributeGroup(titlePaneAttributeGroup);
    }

    private void createMeshAttribute(Mesh mesh, List<Attribute> attributes, List<MeshAttributeBuilder> meshAttributeBuilders) {
        for (MeshAttributeBuilder meshAttributeBuilder : meshAttributeBuilders) {
            if (meshAttributeBuilder.isHandle(mesh)) {
                meshAttributeBuilder.attribute(attributes, mesh);
            }
        }
    }


}
